/************************************************************
 * Project Name		[Thinking_In_Java]
 * File Name     	[pkg_05_swing.Example_04_Font.java]
 * Creation Date 	[21-Jun-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_05_swing;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.font.FontRenderContext;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * Java 字体测试
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Example_04_Font {

	/**
	 * 获得可用的系统字体
	 * 
	 * @return 所有系统字体的名称
	 */
	public static String[] getSystemFontNames() {
		String[] systemFontNames = GraphicsEnvironment
				.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
		return systemFontNames;
	}

	/**
	 * 测试方法
	 * 
	 * @param args
	 *            命令行参数
	 */
	public static void main(String[] args) {
		String[] systemFontNames = Example_04_Font.getSystemFontNames();
		System.out.println("System font type number:" + systemFontNames.length);
		for (String systemFontName : systemFontNames) {
			System.out.println(systemFontName);
		}

		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				FontFrame frame = new FontFrame();
				frame.setVisible(true);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setResizable(true);
			}
		});
	}
}

/**
 * 展示字体的窗体
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 */
class FontFrame extends JFrame {

	private static final long serialVersionUID = -799839958616242786L;

	/**
	 * 构造方法
	 */
	public FontFrame() {
		this.setTitle("Java Font");
		this.setSize(300, 200);
		this.setLocationByPlatform(true);
		this.getContentPane().add(new JPanel() {
			private static final long serialVersionUID = 653677117403781130L;

			@Override
			protected void paintComponent(Graphics g) {
				Graphics2D g2 = (Graphics2D) g;
				String message = "Hello World"; // 要显示的消息

				// 设置字体
				Font font = new Font("Serif", Font.BOLD, 36);
				g2.setFont(font);

				FontRenderContext context = g2.getFontRenderContext();// 衡量message的大小
				// getStringBounds()方法返回包围这个字符串的矩形
				Rectangle2D bounds = font.getStringBounds(message, context);

				double x = (this.getWidth() - bounds.getWidth()) / 2;
				double y = (this.getHeight() - bounds.getHeight()) / 2;

				double ascent = -bounds.getY();
				double baseY = y + ascent;

				// 绘制消息
				g2.drawString(message, (int) x, (int) baseY);
				// 绘制基线
				g2.setPaint(Color.RED);
				g2.draw(new Line2D.Double(x, baseY, x + bounds.getWidth(),
						baseY));
				// 绘制内接矩形
				g2.draw(new Rectangle2D.Double(x, y, bounds.getWidth(), bounds
						.getHeight()));
			}
		});
	}
}